<script type="text/javascript">
	function TermCtrl(CtrlUtil, $http, $element, $injector, $q, modalService) {
		var ctrl = this;
		ctrl.pageNumber = 0;
		ctrl.qs = -1;

		ctrl.onQuerySuccess = function (r) {
			var ids = CtrlUtil.getIds(r.content)
			return $q.all($http.post(':root/term/getApplyCount', {
				term: ids
			}).success(function (R) {
				CtrlUtil.addProperty(r.content, R, 'applyCount')
			}), $http.post(':root/term/getApplyCount', {
				term: ids,
				status: 3
			}).success(function (R) {
				CtrlUtil.addProperty(r.content, R, 'passCount')
			}))
		}
		CtrlUtil.build(ctrl, ':root/term/queryTerms')
		this.dialogPop = function (ediatable, term, option) {
			var q = $q.defer();
			var content = $element.find('.tpl>form').clone()
			var method = ediatable ? 'confirm' : 'show'
			modalService[method]({
				title: option ? '添加' : ediatable ? '修改' : '查看',
				content: content,
				term: term,
				changeQs: function (param) {
					$http.post(':root/term/getCurrent', { trainType: param }).then(function (response) {
						term.qs = ((response.data && response.data.qs) + 1)
					})
				},
				edit: option ? false : true,
				readOnly: !ediatable,
				type: this.type.getData()
			}, option).then(function (r) {
				q.resolve(term);
			})
			return q.promise
		}
		this.dialogAdd = function () {
			var term = {
				openStu: 1,
				status: 1,
				pubTime: new Date()
			}
			ctrl.dialogPop(true, term, {
				backdrop: false
			}).then(function (r) {
				$http.post(':root/term/addTerm', term).success(function (r) {
					if (r) {
						$injector.get('modalService').succ('添加成功', 1500)
						ctrl.current = r
						ctrl.query()
					}
				})
			})
		}
		this.dialogSee = function (term) {
			ctrl.dialogPop(false, term)
		}
		this.dialogEdit = function (term) {
			ctrl.dialogPop(true, term).then(function (r) {
				$http.post(':root/term/updateTerm', term).success(function (r) {
					if (r) {
						$injector.get('modalService').succ('修改成功', 1500)
						ctrl.query()
					}
				})
			})
		}
	}

</script>
<!-- Search area -->
<div ng-controller="TermCtrl as ctrl" ng-init="ctrl.query()">
	<data entity="TrainType" bind-ctrl="ctrl.type" auto-load="true" page="false"></data>
	<div class="panel panel-cys">
		<div class="panel-heading">
			<h3 class="panel-title">
				<span class="glyphicon glyphicon-search"></span> 信息查询
			</h3>
		</div>
		<div class="panel-body">
			<div class="container-fluid">
				<form class="form-horizontal row">
					<div class="form-group col-md-4 remove-margin-horizontal">
						<div class="col-md-10 col-md-offset-1">
							<select chosen ng-model="ctrl.trainTypeQueryParam" ng-options="t.id as t.name for t in ctrl.type.getData()" class="form-control">
								<option value="">培训类别</option>
							</select>

						</div>
					</div>
					<div class="form-group col-md-4 remove-margin-horizontal">
						<div class="col-md-10 col-md-offset-1">
							<input placeholder="期次名称" ng-model="ctrl.nameQueryParam" type="text" class="form-control">
						</div>
					</div>
					<div class="form-group col-md-4 remove-margin-horizontal">
						<div class="col-md-10 col-md-offset-1">
							<select chosen ng-model="ctrl.openStuQueryParam" class="form-control">
								<option value="">是否开放学生申请</option>
								<option value="1">是</option>
								<option value="0">否</option>
							</select>

						</div>
					</div>
					<div class="form-group col-md-4 remove-margin-horizontal">
						<div class="col-md-10 col-md-offset-1">
							<select chosen ng-model="ctrl.statusQueryParam" class="form-control">
								<option value="">选择启用状态</option>
								<option value="1">是</option>
								<option value="0">否</option>
							</select>
						</div>
					</div>
				</form>
			</div>
			<div class="row pull-right">
				<div class="col-md-12">
					<button ng-click="ctrl.query()" type="submit" class="btn btn-cys btn-sm">
						<span class="glyphicon glyphicon-search"></span>
						查询
					</button>
					<button ng-click="ctrl.reset()" type="reset" class="btn btn-default btn-sm">
						<span class="glyphicon glyphicon-repeat"></span>
						重置
					</button>
				</div>
			</div>
		</div>
	</div>
	<!-- Search area END -->

	<!-- operation area -->
	<a type="button" ng-click="ctrl.dialogAdd()" class="btn btn-cys btn-sm">
		<span class="glyphicon glyphicon-plus"></span>
		新增
	</a>
	<div class="ny-top-r pull-right" ng-include="':root/components/page.html'"></div>
	<!-- operation area END -->

	<!-- 期次新增、编辑弹出框 -->



	<!-- data grid area -->
	<div style="padding-top: 10px;">
		<table class="table table-bordered table-hover table-striped text-center">
			<tr>
				<th>类别</th>
				<th>期次</th>
				<th>期数</th>
				<th>开放申请</th>
				<th>申请开始时间</th>
				<th>申请结束时间</th>
				<th>名额</th>
				<th>报名人数</th>
				<th>通过人数</th>
				<th>发布时间</th>
				<th>状态</th>
				<th>操作</th>
			</tr>
			<tr ng-repeat="item in ctrl.entities.content">
				<td>{{item.trainType.name}}</td>
				<td style="width: 80px; max-width: 80px; white-space: nowrap; text-overflow: ellipsis; overflow: hidden;" title="{{item.name}}">{{item.name}}</td>
				<td>{{item.qs}}</td>
				<td>{{item.openStu|toBool}}</td>
				<td title="{{item.startTime|date:'yyyy-MM-dd HH:mm'}}">{{item.startTime|date:'yyyy-MM-dd'}}</td>
				<td title="{{item.endTime|date:'yyyy-MM-dd HH:mm'}}">{{item.endTime|date:'yyyy-MM-dd'}}</td>
				<td>{{item.mers}}</td>
				<td>{{item.applyCount}}</td>
				<td>{{item.passCount}}</td>
				<td title="{{item.pubTime|date:'yyyy-MM-dd HH:mm'}}">{{item.pubTime|date:'yyyy-MM-dd'}}</td>
				<td class="{{item.status==0?'text-danger':'text-success'}}">{{item.status==0?'禁用':'启用'}}</td>
				<td>
					<a href="javascript:;" class="remove-underline" ng-click="ctrl.dialogSee(item)">查看</a>
					<a ng-show="item.status==1" href="javascript:;" class="remove-underline" ng-click="ctrl.dialogEdit(item)">编辑</a>
				</td>
			</tr>
		</table>
		<p>
			共有记录：
			<span>{{ctrl.entities.totalElements}}</span> 条
		</p>
	</div>
	<!-- data grid area END -->


	<div ng-non-bindable class="tpl hidden"  ng-if="term">
		<form class="form-horizontal">
			<div class="form-group">
				<label class="col-md-2 control-label">培训类别</label>
				<div class="col-md-4" ng-if="type">
					<select ng-disabled="edit||readOnly" ng-change="changeQs(term.trainType.id)" class="form-control" ng-options="m.id as m.name for m in type" ng-model="term.trainType.id">
					</select>
				</div>
				<label class="col-md-2 control-label">期次名称</label>
				<div class="col-md-4">
					<input ng-readonly="readOnly" ng-model="term.name" type="text" class="form-control" name="name" required="required">
				</div>
			</div>

			<div class="form-group">
				<label class="col-md-2 control-label">期数</label>
				<div class="col-md-4">
					<input ng-readonly="readOnly" ng-model="term.qs" type="number" required="required" name="qs" class="form-control">
				</div>
				<label class="col-md-2 control-label">开放申请</label>
				<div class="col-md-4">
					<select ng-disabled="readOnly" ng-model="term.openStu" class="form-control">
						<option ng-value="1">是</option>
						<option ng-value="0">否</option>
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-md-2 control-label">申请开始时间</label>
				<div class="col-md-4">
					<input ng-readonly="readOnly" datetimepicker ng-model="term.startTime" type="text" required="required" name="startTime" class="form-control">
				</div>
				<label class="col-md-2 control-label">申请结束时间</label>
				<div class="col-md-4">
					<input ng-readonly="readOnly" datetimepicker ng-model="term.endTime" type="text" required="required" name="endTime" class="form-control">
				</div>
			</div>
			<div class="form-group">
				<label class="col-md-2 control-label">设定名额</label>
				<div class="col-md-4">
					<input ng-readonly="readOnly" ng-model="term.mers" type="number" required="required" name="mers" class="form-control">
				</div>
				<label class="col-md-2 control-label">发布时间</label>
				<div class="col-md-4">
					<input ng-readonly="readOnly" datetimepicker ng-model="term.pubTime" type="text" name="pubTime" class="form-control">
				</div>
			</div>
			<div class="form-group">
				<label class="col-md-2 control-label">启用状态</label>
				<div class="col-md-4">
					<select ng-disabled="readOnly" ng-model="term.status" class="form-control">
						<option ng-value="1">是</option>
						<option ng-value="0">否</option>
					</select>
				</div>
			</div>
			<div class="form-group">
				<data entity="TermIntro" auto-load="true" after-refresh="term.intro=data[0].intro">
					<filter field="term" value="term.id"></filter>
				</data>
				<label class="col-md-2 control-label">简介</label>
				<div class="col-md-10">
					<textarea ng-disabled="readOnly" rows="4" style="resize:none;" ng-model="term.intro" class="form-control">
					</textarea>
				</div>
			</div>
		</form>
	</div>
</div>